<template>
  <el-dialog
    :title="!dataForm.brokerHomeId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="图片链接" prop="homeImage">
      <el-input v-model="dataForm.homeImage" placeholder="图片链接"></el-input>
    </el-form-item>
    <el-form-item label="跳转地址" prop="homeUrl">
      <el-input v-model="dataForm.homeUrl" placeholder="跳转地址"></el-input>
    </el-form-item>
    <el-form-item label="sort排序" prop="homeSort">
      <el-input v-model="dataForm.homeSort" placeholder="sort排序"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除 1是 0 否（逻辑删除）" prop="delStatus">
      <el-input v-model="dataForm.delStatus" placeholder="是否删除 1是 0 否（逻辑删除）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          brokerHomeId: 0,
          title: '',
          homeImage: '',
          homeUrl: '',
          homeSort: '',
          createTime: '',
          updateTime: '',
          delStatus: ''
        },
        dataRule: {
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          homeImage: [
            { required: true, message: '图片链接不能为空', trigger: 'blur' }
          ],
          homeUrl: [
            { required: true, message: '跳转地址不能为空', trigger: 'blur' }
          ],
          homeSort: [
            { required: true, message: 'sort排序不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          delStatus: [
            { required: true, message: '是否删除 1是 0 否（逻辑删除）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.brokerHomeId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.brokerHomeId) {
            this.$http({
              url: this.$http.adornUrl(`/broker/brokerroombrokerhome/info/${this.dataForm.brokerHomeId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.brokerRoomBrokerHome.title
                this.dataForm.homeImage = data.brokerRoomBrokerHome.homeImage
                this.dataForm.homeUrl = data.brokerRoomBrokerHome.homeUrl
                this.dataForm.homeSort = data.brokerRoomBrokerHome.homeSort
                this.dataForm.createTime = data.brokerRoomBrokerHome.createTime
                this.dataForm.updateTime = data.brokerRoomBrokerHome.updateTime
                this.dataForm.delStatus = data.brokerRoomBrokerHome.delStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/broker/brokerroombrokerhome/${!this.dataForm.brokerHomeId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'brokerHomeId': this.dataForm.brokerHomeId || undefined,
                'title': this.dataForm.title,
                'homeImage': this.dataForm.homeImage,
                'homeUrl': this.dataForm.homeUrl,
                'homeSort': this.dataForm.homeSort,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'delStatus': this.dataForm.delStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
